﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using UtilPublic.ServerLoger;
using StackExchange.Redis;
using System.Collections.Generic;

namespace UtilPublic.Redis
{
    /// <summary>
    /// 排行榜公共数据
    /// </summary>
    public class RdsTopData
    {
        /// <summary>
        /// 排行
        /// </summary>
        public int Rank;
        /// <summary>
        /// 对象唯一ID
        /// </summary>
        public string Puid;
        /// <summary>
        /// 得分
        /// </summary>
        public double Score;
        /// <summary>
        /// 对象数据
        /// </summary>
        public string Data;
    }

    /// <summary>
    /// SortedSet存储 集成排行榜功能
    /// </summary>
    public partial class Rds
    {
        private string TopKey<T>(T key) => $"{Zk(key)}:Top";
        private string TopData<T>(T key) => $"{Zk(key)}:Data";
        /// <summary>
        /// 加入
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="score"></param>
        /// <param name="data"></param>
        public void TopSet<T>(string key, T value, double score, string data)
        {
            RedisDb.SortedSetAdd(TopKey(key), $"{value}", score);
            RedisDb.HashSet(TopData(key), $"{value}", data);
        }
        /// <summary>
        /// 增量
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="score"></param>
        /// <param name="data"></param>
        public void TopInc<T>(string key, T value, double score, string data)
        {
            RedisDb.SortedSetIncrement(TopKey(key), $"{value}", score);
            RedisDb.HashSet(TopData(key), $"{value}", data);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void TopRemove<T>(string key, T value) => RedisDb.SortedSetRemove(TopKey(key), $"{value}");
        /// <summary>
        /// 获取排行 默认降序
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="Descending">是否降序</param>
        /// <returns></returns>
        public long TopRank<T>(string key, T value, bool Descending = true)
        {
            var order = Descending ? Order.Descending : Order.Ascending;
            var result = RedisDb.SortedSetRank(TopKey(key), $"{value}", order);
            if (result == null)
            {
                return -1;
            }
            else
            {
                return (long)result + 1;
            }
        }
        /// <summary>
        /// 获取列表 根据排名 默认降序
        /// </summary>
        /// <param name="key"></param>
        /// <param name="start"></param>
        /// <param name="stop"></param>
        /// <param name="Descending">是否降序</param>
        /// <returns></returns>
        public List<RdsTopData> TopGetByRank(string key, int start, int stop, bool Descending = true)
        {
            var list = new List<RdsTopData>();
            try
            {
                var order = Descending ? Order.Descending : Order.Ascending;
                var rr = RedisDb.SortedSetRangeByRankWithScores(TopKey(key), start, stop, order);
                int rank = 1;
                foreach (var nn in rr)
                {
                    RdsTopData top = new();
                    top.Rank = rank;
                    top.Puid = nn.Element;
                    top.Score = nn.Score;
                    top.Data = RedisDb.HashGet(TopData(key), top.Puid);
                    list.Add(top);
                    rank++;
                }
            }
            catch
            {
                Zloger.Fatal($"StSetRangeByRank Error {key} {start} {stop}");
            }
            return list;
        }
        /// <summary>
        /// 获取列表 根据页码 默认降序
        /// </summary>
        /// <param name="key"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageCount"></param>
        /// <param name="Descending">是否降序</param>
        /// <returns></returns>
        public List<RdsTopData> TopGetByPage(string key, int pageIndex = 0, int pageCount = 100, bool Descending = true)
        {
            int start = pageCount * pageIndex;
            int stop = (pageIndex + 1) * pageCount - 1;
            return TopGetByRank(key, start, stop, Descending);
        }
        
    }
}